package org.wonders.statemachine.config;

import com.alibaba.cola.statemachine.Condition;
import com.alibaba.cola.statemachine.builder.StateMachineBuilder;
import com.alibaba.cola.statemachine.builder.StateMachineBuilderFactory;
import org.wonders.statemachine.enums.OrderStatusChangeEventEnum;
import org.wonders.statemachine.enums.OrderStatusEnum;

/**
 * 阿里状态机Cola
 */
public class OrderStateMachineBuilder {

    public void buildStateMachine() {
        StateMachineBuilder<OrderStatusEnum, OrderStatusChangeEventEnum, OrderContext> builder = StateMachineBuilderFactory.create();
        builder.externalTransition()
                .from(OrderStatusEnum.WAIT_PAYMENT)
                .to(OrderStatusEnum.WAIT_DELIVER)
                .on(OrderStatusChangeEventEnum.PAYED)
                .when(matchCondition())
                .perform((from, to, event, context) -> {
                    // 创建订单的业务逻辑
                });
        builder.build("orderStateMachine");
    }

    private Condition<OrderContext> matchCondition() {
        return orderContext -> true; // 实际业务中需要实现具体逻辑
    }


}
